<template>
<div class="box">
  <div class="main">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <div><h3>迷你博客</h3></div>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import {login} from "@/api/user";
import {EventBus} from "@/pages/components/eventBus";

export default {
  name: 'Login',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass0 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      flag: false,
      ruleForm: {
        name: '',
        pass: '',
        checkPass: '',
      },
      tempForm: {
        username:'',
        password:''
      },
      rules: {
        name: [
          { validator: validatePass0, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ifSuccess()
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    ifSuccess() {
      this.tempForm.username = this.ruleForm.name
      this.tempForm.password = this.ruleForm.pass
      login(this.tempForm).then((res) => {
        res = res.data
        if (res.code === 200)
        {
          EventBus.$emit('aMsg', this.tempForm)
          this.flag = true
          this.$alert('登录成功', '提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$message({
                type: 'info',
                message: `action: ${ action }`
              });
            }
          });
          this.$router.replace('/Home').catch(err=>err);
        } else{
          this.flag = false
          alert('用户名或密码错误，请重新登录');
        }
      }).catch(() => {
        alert('接口请求失败');
        this.flag = true
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.box {
  display: flex;
  justify-content: center;
  margin-top: 200px;
}
.main {
  border-radius: 20px;
  padding-top: 20px;
  padding-right: 40px;
  background-color: #39d3ee;
}
</style>
